package com.kmt.plaza.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kmt.plaza.entity.ShoppingCart;
import com.kmt.plaza.entity.User;
import com.kmt.plaza.sevice.ShoppingCartService;
import com.kmt.plaza.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shopCart")
@CrossOrigin
@Api(value = "提供购物车相关接口",tags="购物车管理")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private ObjectMapper objectMapper = new ObjectMapper();

//    @GetMapping("/list")
//    @ApiImplicitParams({
//            @ApiImplicitParam(dataType = "String",name = "token",value = "授权令牌",required =true),
//    })
//    public ResultVO listCart(String token){
//        //1、获取token
//        //2、校验token
//        if(token == null){
//            return new ResultVO(ResStatus.NO,"请先登录",null);
//        }else{
//            //验证token
//            JwtParser parser = Jwts.parser();
//            parser.setSigningKey("kmtwangwei");
//            try {
//                //如果token正确(密码正确、有效期内)则正常执行
//                Jws<Claims> claimsJws = parser.parseClaimsJws(token);
//                //
//                Claims body = claimsJws.getBody();//过去token中用户的数据
//                String subject = body.getSubject();
//                String v1 = body.get("key1",String.class);
//                return new ResultVO(ResStatus.OK,"success",null);
//            }catch (Exception e){
//                return new ResultVO(ResStatus.OK,"登陆过期，请重新登录",null);
//            }
//        }
//    }

    @PostMapping("/add")
    @ApiOperation("添加购物车接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String",name = "token",value = "授权令牌",required =true),
    })
    public ResultVO addCart(@RequestBody ShoppingCart shoppingCart, @RequestHeader("token")String token) throws JsonProcessingException {
        ResultVO resultVO = shoppingCartService.addShoppingCart(shoppingCart);
        String s = stringRedisTemplate.boundValueOps(token).get();
        objectMapper.readValue(s, User.class);
        return resultVO;
    }

    @GetMapping("/list")
    @ApiOperation("查询购物车列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int",name = "userId",value = "用户id",required =true),
    })
    public ResultVO list(Integer userId,@RequestHeader("token")String token){
        ResultVO resultVO = shoppingCartService.listShoppingCartByUserId(userId);
        return resultVO;
    }

    @GetMapping("/listByCids")
    @ApiOperation("查询购物车记录列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String",name = "cids",value = "购物车记录列表接口",required =true),
    })
    public ResultVO listByCids(String cids,@RequestHeader("token")String token){
        ResultVO resultVO = shoppingCartService.listShoppingCartByCids(cids);
        return resultVO;
    }
    @ApiOperation(value = "删出购物车商品接口")
    @DeleteMapping(value = "/deleteShopCart/{cartId}")
    public ResultVO deleteShopCart(@PathVariable("cartId") Integer cartId,@RequestHeader("token")String token) {
        return shoppingCartService.deleteShoppingCart(cartId);
    }

}
